<?

/***************************************************************************
 Jupiter Content System @ Jupiterportal.com
 Copyright (C) 2005 Cosmin Flavius (highstrike@gmail.com)

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 More Info About The Licence At http://www.gnu.org/copyleft/gpl.html
****************************************************************************/

//-----------------------------------------------------------------
//  Filter Acces Rightes
//-----------------------------------------------------------------

if(isset($is_guest) || isset($is_user) || isset($is_moderator))
{ header("location: $PHP_SELF?i=2"); exit; }

switch($a)
{
	//-----------------------------------------------------------------
	//  Add ip
	//-----------------------------------------------------------------

	case 1:

		//-----------------------------------------------------------------
		//  Check for Ip
		//-----------------------------------------------------------------

		if(!$addip)
		{
			header("location: $PHP_SELF?n=modules/ban&i=39");
			exit;
		}

		//-----------------------------------------------------------------
		//  Check if Ip exists in database
		//-----------------------------------------------------------------

		$checker = $db->getLine("SELECT * FROM bans WHERE ip = '$addip'");

		if($checker != FALSE)
		{
			header("location: $PHP_SELF?n=modules/ban&i=40");
			exit;
		}

		//-----------------------------------------------------------------
		//  Write log
		//-----------------------------------------------------------------

		$db->insertRow("log",array('user_id' => ''.$_SESSION['id'].'','user_user' => ''.$_SESSION['username'].'','log' => ''.$language['Logs desc3'].' <b>bans</b> the ip -> \''.$addip.'\'','date' => ''.time().''));

		//-----------------------------------------------------------------
		//  Write the ip in the table
		//-----------------------------------------------------------------

		$db->insertRow("bans",array('user_id' => ''.$_SESSION['id'].'','user_user' => ''.$_SESSION['username'].'','banned_id' => NULL,'banned_user' => NULL,'ip' => ''.$addip.'','date' => ''.time().''));

		//-----------------------------------------------------------------
		//  Redirect when finished + trick
		//-----------------------------------------------------------------

		$rows_per_page = $config['nav_bans'];
		$temp = $db->getTable("SELECT * FROM bans");
		$numrows = count($temp);

		$lastpage = ceil($numrows/$rows_per_page);

		header("location: $PHP_SELF?n=modules/ban&s=1&t=ASC&p=$lastpage&i=41");

	break;

	//-----------------------------------------------------------------
	//  Unblock ip
	//-----------------------------------------------------------------

	case 2:

		//-----------------------------------------------------------------
		//  Write Log
		//-----------------------------------------------------------------

		$checker = $db->getLine("SELECT * FROM bans WHERE id = '$d'");
		$db->insertRow("log",array('user_id' => ''.$_SESSION['id'].'','user_user' => ''.$_SESSION['username'].'','log' => ''.$language['Logs desc'].' <b>bans</b> the ip -> \''.$checker['ip'].'\'','date' => ''.time().''));

		//-----------------------------------------------------------------
		//  Delete from list
		//-----------------------------------------------------------------

		$db->deleteRow("bans","id = '$d'");

		//-----------------------------------------------------------------
		//  Redirect when finished
		//-----------------------------------------------------------------

		header("location: $PHP_SELF?n=modules/ban&s=$s&t=$t&p=$p&i=42");

	break;

	//-----------------------------------------------------------------
	//  Unban user
	//-----------------------------------------------------------------

	case 3:

		//-----------------------------------------------------------------
		//  Write Log
		//-----------------------------------------------------------------

		$checker = $db->getLine("SELECT * FROM bans WHERE banned_id = '$d'");
		$db->insertRow("log",array('user_id' => ''.$_SESSION['id'].'','user_user' => ''.$_SESSION['username'].'','log' => ''.$language['Logs desc'].' <b>bans</b> the user -> \''.$checker['banned_user'].'\'','date' => ''.time().''));
		$db->insertRow("log",array('user_id' => ''.$_SESSION['id'].'','user_user' => ''.$_SESSION['username'].'','log' => ''.$language['Logs desc2'].' <b>users</b> the user -> \''.$checker['banned_user'].'\' to status = \'ready\'','date' => ''.time().''));

		//-----------------------------------------------------------------
		//  Delete from list
		//-----------------------------------------------------------------

		$db->deleteRow("bans","banned_id = '$d'");
		$db->updateRow("users",array('status' => 'ready'),"id = $d");

		//-----------------------------------------------------------------
		//  Redirect when finished
		//-----------------------------------------------------------------

		header("location: $PHP_SELF?n=modules/ban&s=$s&t=$t&p=$p&i=43");

	break;

	//-----------------------------------------------------------------
	//  Show list + form
	//-----------------------------------------------------------------

	default:

		//-----------------------------------------------------------------
		//  Navigation Stuff
		//-----------------------------------------------------------------

		$rows_per_page = $config['nav_bans'];
		$temp = $db->getTable("SELECT * FROM bans");
		$numrows = count($temp);

		$lastpage = ceil($numrows/$rows_per_page);
		$p = (int)$p;

		if ($p < 1) $p = 1;
		elseif ($p > $lastpage) $p = $lastpage;

		if($numrows != FALSE) $limit = 'LIMIT ' .($p - 1) * $rows_per_page .',' .$rows_per_page;
		else $limit = NULL;

		//-----------------------------------------------------------------
		//  Sorting Stuff
		//-----------------------------------------------------------------

		if(!isset($s)) $s = 'id'; // Default value
		if(!isset($t)) $t = 'ASC'; // Default value

		$sname1 = "id";
		$sname2 = "ip";
		$sname3 = "banned_user";
		$sname4 = "date";
		$sname5 = "user_user";

		if($t == "ASC"){ $t = "DESC"; $t2 = "ASC"; }
		else{ $t = "ASC"; $t2 = "DESC"; }

		if($t == "ASC"){ $t3 = "<img src='templates/$template/images/misc/up.gif' border='0'>";}
		else{ $t3 = "<img src='templates/$template/images/misc/down.gif' border='0'>";}

		if($s == 1) $s = $sname1;
		if($s == 2) $s = $sname2;
		if($s == 3) $s = $sname3;
		if($s == 4) $s = $sname4;
		if($s == 5) $s = $sname5;

		//-----------------------------------------------------------------
		//  Load the bans table
		//-----------------------------------------------------------------

		$bans = $db->getTable("SELECT * FROM bans ORDER BY '$s' $t2 $limit");

		//-----------------------------------------------------------------
		//  Make a foreach to separate the results
		//-----------------------------------------------------------------

		if($bans != FALSE)
		{
			foreach($bans as $idx=>$ban)
			{
				//-----------------------------------------------------------------
				//  Count for ID + trick
				//-----------------------------------------------------------------

				if($s == $sname1 && $t == "ASC")
					$idx_count = str_replace("-", "", $idx + (($p - 1) * $rows_per_page + 1) - $numrows - 1);
				else
					$idx_count = $idx + (($p - 1) * $rows_per_page + 1);

				//-----------------------------------------------------------------
				//  Check see if he is auto protection bot or regular ip
				//-----------------------------------------------------------------

				if($ban['user_user'] == "Auto Protection")
					$added_by = "{$ban['user_user']}";
				else
					$added_by = "<a href='$PHP_SELF?n=modules/users&a=1&d={$ban['user_id']}'>{$ban['user_user']}</a>";

				//-----------------------------------------------------------------
				//  Make Layout
				//-----------------------------------------------------------------

				if(!$ban['banned_id'] && !$ban['banned_user'])
					$layout .= "<tr class='".row2color($idx)."' onMouseOver=this.className='con3' onMouseOut=this.className='".row2color($idx)."' onclick=window.location.href='http://www.dnsstuff.com/tools/whois.ch?ip={$ban['ip']}'>";
				else
					$layout .= "<tr class='".row2color($idx)."' onMouseOver=this.className='con3' onMouseOut=this.className='".row2color($idx)."' onclick=window.location.href='$PHP_SELF?n=modules/users&a=1&d={$ban['banned_id']}'>";

				$layout .= "
					<td align='center'>$idx_count</td>
					<td><a href='http://www.dnsstuff.com/tools/whois.ch?ip={$ban['ip']}' title='{$language['Ban view desc5']}'>{$ban['ip']}</a></td>
					<td><a href='$PHP_SELF?n=modules/users&a=1&d={$ban['banned_id']}'>{$ban['banned_user']}</td>
					<td>".today_yesterday($ban['date'])."</td>
					<td>$added_by</td>";

				if(!$ban['banned_id'] && !$ban['banned_user'])
					$layout .= "<td><a href='$PHP_SELF?n=modules/ban&a=2&d={$ban['id']}&s=$s&t=$t2&p=$p'><img src='templates/$template/images/buttons/delete.gif' border='0' alt='{$language['Ban view desc6']} {$ban['ip']}'></a></td>";
				else
					$layout .= "<td><a href='$PHP_SELF?n=modules/ban&a=3&d={$ban['banned_id']}&s=$s&t=$t2&p=$p'><img src='templates/$template/images/buttons/unban.gif' border='0' alt='{$language['Ban view desc7']} {$ban['banned_user']}'></a></td>";

				$layout .= "</tr>\n";

				//-----------------------------------------------------------------
				//  Start from 1
				//-----------------------------------------------------------------

				$idx ++;
			}
		}
		else
		{
			//-----------------------------------------------------------------
			//  Reset to 0
			//-----------------------------------------------------------------

			$idx = 0;

			//-----------------------------------------------------------------
			//  Make Layout
			//-----------------------------------------------------------------

			$layout .= "<tr><td class='con1' colspan='6'>{$language['Ban view desc8']}</td></tr>\n";
		}

		//-----------------------------------------------------------------
		//  Navigation Code
		//-----------------------------------------------------------------

		if ($p == 1) $nav_prev = "<tr><td class='con1' align='right'>&nbsp;</td>";
		else
		{
			$prevpage = $p-1;
			$nav_prev = "<tr><td class='con1' align='left'><a href='$PHP_SELF?n=modules/ban&s=$s&t=$t2&p=$prevpage'>&laquo; {$language['Ban previous']}</a></td>";
		}

		$nav_nr = '[ '.paginate($lastpage, $p, $PHP_SELF.'?n=modules/ban&s='.$s.'&t='.$t2).' ]';

		if($idx == $numrows) $nav_nr = NULL;

		if ($p == $lastpage)
			$nav_next = "<td class='con1' align='right'>&nbsp;</td></tr>";
		else
		{
			$nextcode = $numrows - $idx * $p;
			if($nextcode > $idx) $how_next = $idx;
			else $how_next = $nextcode;

			$nextpage = $p+1;

			$nav_next = "<td class='con1' align='right'><a href='$PHP_SELF?n=modules/ban&s=$s&t=$t2&p=$nextpage'>{$language['Ban next']} $how_next &raquo;</a></td></tr>";
		}

		if($idx == $numrows)
		{
			$nav_head = "class='con1'";
			$nav = NULL;
		}
		else
		{
			$nav_head = "class='head'";
			$nav = ''.$nav_prev.''.$nav_next.'';
		}

		//-----------------------------------------------------------------
		// Echo the back link
		//-----------------------------------------------------------------

		echo "<table class='main' cellspacing='1' cellpadding='4'><tr class='head'><td colspan='3'>{$language['Admin name']}</td></tr><tr><td colspan='3' class='con1' onMouseOver=this.className='con3' onMouseOut=this.className='con1' onclick=window.location.href='$PHP_SELF?n=modules/admin'><a href='$PHP_SELF?n=modules/admin'> &raquo; {$language['Admin back']}</a></td></tr></table><br>";

		//-----------------------------------------------------------------
		//  Start Layout + Navigation
		//-----------------------------------------------------------------

		?>
			<form method="post" action="<?= $PHP_SELF ?>?n=modules/ban">
			<table class="main" cellspacing="1" cellpadding="4" width="100%">
			<col width="1%"><col width="19%"><col width="20%"><col width="40%"><col width="19%"><col width="1%">
			<tr class='head'><td colspan='6'><?= $language['Ban add title'] ?></td></tr>
			<tr class="bottom">
			<td colspan="6"><?= $language['Ban add title2'] ?></td>
			</tr>
			<tr>
			<td class="con1" colspan="4" width="100%"><?= $language['Ban add desc'] ?> <input type="text" maxlength="15" style="width:150" class="box" name="addip"></td>
			<td class="con1" colspan="2" width="1%" align="right"><input type="submit" style="width:100" class="box" value="<?= $language['Ban add desc2'] ?>"></td>
			</tr>
			</table>
			<input type="hidden" name="a" value="1">
			</form>
			<br>
			<table class="main" cellspacing="1" cellpadding="4" width="100%">
			<col width="1%"><col width="19%"><col width="20%"><col width="40%"><col width="19%"><col width="1%">
			<tr class='head'><td colspan='6'><?= $language['Ban view title'] ?></td></tr>
			<tr class="bottom">
			<td align="center"><a href="<?= $PHP_SELF ?>?n=modules/ban&s=1&t=<?= $t ?>&p=<?= $p ?>"><? if($s == $sname1) echo $t3; else echo "#" ?></a></td>
			<td><a href="<?= $PHP_SELF ?>?n=modules/ban&s=2&t=<?= $t ?>&p=<?= $p ?>"><?= $language['Ban view desc'] ?> <? if($s == $sname2) echo $t3 ?></a></td>
			<td><a href="<?= $PHP_SELF ?>?n=modules/ban&s=3&t=<?= $t ?>&p=<?= $p ?>"><?= $language['Ban view desc2'] ?> <? if($s == $sname3) echo $t3 ?></a></td>
			<td><a href="<?= $PHP_SELF ?>?n=modules/ban&s=4&t=<?= $t ?>&p=<?= $p ?>"><?= $language['Ban view desc3'] ?> <? if($s == $sname4) echo $t3 ?></a></td>
			<td><a href="<?= $PHP_SELF ?>?n=modules/ban&s=5&t=<?= $t ?>&p=<?= $p ?>"><?= $language['Ban view desc4'] ?> <? if($s == $sname5) echo $t3 ?></a></td>
			<td>&nbsp;</td>
			</tr>
			<?= $layout ?>
			</table>
			<br>
			<table class="main" cellspacing="1" cellpadding="4" width="100%">
			<col width="50%"><col width="50%">
			<tr <?= $nav_head ?>>
			<td colspan="2">
			<table border="0" cellspacing="0" cellpadding="0" width="100%">
			<col width="50%"><col width="50%">
			<tr class="empty">
			<td align="left"><?= $language['Ban nav'] ?> <b><?= $idx ?></b> <?= $language['Ban nav2'] ?> <b><?= $numrows ?></b></td>
			<td align="right"><?= $nav_nr ?></td>
			</tr>
			</table>
			</td>
			</tr>
			<?= $nav ?>
			</table>
		<?
}
?>